﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BizAppFactory.Domain
{
    public partial class Role
    {
        string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        AssociationMultiplicityEnum _Multiplicity;
        public AssociationMultiplicityEnum Multiplicity
        {
            get { return _Multiplicity; }
            set { _Multiplicity = value; }
        }

        DomainClass _DomainClass;

        public DomainClass DomainClass
        {
            get { return _DomainClass; }
            set { _DomainClass = value; }
        }

        string _PeerRoleName;
        public string PeerRoleName
        {
            get { return _PeerRoleName; }
            set { _PeerRoleName = value; }
        }

        AssociationMultiplicityEnum _PeerRoleMultiplicity;
        public AssociationMultiplicityEnum PeerRoleMultiplicity
        {
            get { return _PeerRoleMultiplicity; }
            set { _PeerRoleMultiplicity = value; }
        }

        RoleType _RoleType;
        public RoleType RoleType
        {
            get { return _RoleType; }
            set { _RoleType = value; }
        }

        Association _Association;

        public Association Association
        {
            get { return _Association; }
            set { _Association = value; }
        }

        public bool GeneratesForeignKey
        {
            get 
            {
                if ( // many to many
                        (Multiplicity == AssociationMultiplicityEnum.ZeroOrMany ||
                        Multiplicity == AssociationMultiplicityEnum.OneOrMany)
                        &&
                        (PeerRoleMultiplicity == AssociationMultiplicityEnum.ZeroOrMany||
                        PeerRoleMultiplicity == AssociationMultiplicityEnum.OneOrMany)
                   )
                {
                    return false;
                }
                else
                {
                    return
                        //one to may, many end
                        (
                            (Multiplicity == AssociationMultiplicityEnum.ZeroOrOne ||
                            Multiplicity == AssociationMultiplicityEnum.One)
                            &&
                            (PeerRoleMultiplicity == AssociationMultiplicityEnum.ZeroOrMany ||
                            PeerRoleMultiplicity == AssociationMultiplicityEnum.OneOrMany)
                        )
                        ||

                        // one to one, target end
                        (
                            (Multiplicity == AssociationMultiplicityEnum.ZeroOrOne ||
                            Multiplicity == AssociationMultiplicityEnum.One)
                            &&
                            (PeerRoleMultiplicity == AssociationMultiplicityEnum.ZeroOrOne ||
                            PeerRoleMultiplicity == AssociationMultiplicityEnum.One)
                            &&
                            RoleType == RoleType.Target
                        );
                }
            }
        }
    }
}
